<?php
/*
 * SMS class that runs as cronjob
 * Available sms types:
 *  a. NofityManager - a daily sms sent which indicates summaries input status
 *
] */
class SMSCommand extends CConsoleCommand
{
    // Defines the mail type sent
    private $_smsType='NotifyManager'; // Set default
    private $_centers=array(2);
    private $_smsData=array();

    public function __construct()
    {
        if(isset($args[0]))
            $this->_smsType=$args[0];
        //require_once (Yii::app()->basePath.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'mailing_list.php');
        require_once (Yii::app()->basePath.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'summaries.php');
        //require_once (Yii::app()->basePath.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.'grbac'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'Users.php');
    }
    public function run($args)
    {
     
        switch($this->_smsType){
            case 'NotifyManager':
                $this->NotifyManager();
                break;
        }
        //uncomment for debug
        //print_r($this->_smsData);
        if($this->_smsData)
           $this->sendSMS();
       
           //    $summaries = summaries::model()->findAll("note_date=:note_date",
          //                  array(':note_date'=>date('Y-m-d',time())));
        
    }

    private function sendSMS()
    {
        foreach($this->_smsData as $sms)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
            /*curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);*/
            curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile.txt");
            curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile.txt");
            curl_setopt($curl, CURLOPT_URL, "http://misron.net/send/index.php");
            curl_setopt($curl, CURLOPT_POSTFIELDS,$sms);

            $result = curl_exec ($curl);
            curl_close ($curl);
        }
    }

  /*  private function getRecipients()
    {
        switch($this->_mailType)
        {
            case 'DailyMail':
                $today = getdate(time());
                $offset = 60*60*24*($today['wday']==4?3:1);
                return mailing_list::model()->findAll('assign_date = :assign_date',array(':assign_date' => date('Y-m-d',time()+$offset)));
            default: return NULL;
        }
    }*/

    private function getNotifyManagerMessage()
    {
         $summary = summaries::model()->exists("note_date=:note_date",
                            array(':note_date'=>date('Y-m-d',time())));
         if($summary)
             return "הי אילנה , התקבל סיכום יומי. המשך ערב נעים :)";
         return "הי אילנה , עדיין לא התקבל סיכום יומי.";
    }

    private function NotifyManager(){
        $today = getdate(time());
        if($today['wday']>4) // Send sms only from Sunday till Thursday
            return NULL;
        //$this->createSMSMessage('0546541494',$this->getNotifyManagerMessage());
        $this->createSMSMessage('0546541494',$this->getNotifyManagerMessage());
    }

    private function createSMSMessage($phoneNumber,$msg){
        $smsInfo = array_merge(
                array('uin'=>Yii::app()->params['icqAccount'])
               ,array('uin_password'=>Yii::app()->params['icqPassword'])
               ,array('number'=>$phoneNumber)
               ,array('smstext'=>$msg)
               ,array('posted'=>'true')
                );
        $this->_smsData[]=$this->serializeOptions($smsInfo,'=','&');
    }

    private function serializeOptions($options,$delimiter=':',$suffix=';')
    {
        foreach($options as $key=>&$val)
            $val= "$key$delimiter$val";
        return implode($suffix,$options);
    }

}

?>
